<?php
/**
 * @copyright   Copyright (c) 2017 https://www.sapixx.com All rights reserved.
 * @license     Licensed (http://www.apache.org/licenses/LICENSE-2.0).
 * @author      pillar<ltmn@qq.com>
 * 公司管理
 */
namespace app\allwin\model;
use app\common\facade\Inform;
use think\Model;

class Worker extends Model{
    
    protected $pk     = 'id';
    protected $table  = 'ai_allwin_user_worker';
 
    //和主用户表绑定关系
    public function user(){
        return $this->hasOne('app\common\model\SystemUser','id','uid');
    }

    /**
      * 把已经是员工的用户排除在外返回用户列表
     */
    public static function selects($condition,int $miniapp_id){
        $user = self::where(['member_miniapp_id' => $miniapp_id])->field('uid')->select()->toArray();
        $uid = [];
        if(!empty($user)){
            $uid = array_column($user,'uid');
        } 
        return model('SystemUser')->where($condition)->whereNotIn('id',$uid)->order('id desc')->paginate(10);
    }

    /**
     * 添加编辑
     * @param  array $param 数组
     */
    public static function add(int $member_miniapp_id,array $ids){
        $data = [];
        $user = self::where(['member_miniapp_id' => $member_miniapp_id,'uid' => $ids])->field('uid')->select()->toArray();
        $uid = [];
        if(!empty($user)){
            $uid = array_column($user,'uid');
        }
        foreach ($ids as $key => $value) {
            if(!in_array($value,$uid)){
                $data[$key]['member_miniapp_id'] = $member_miniapp_id;
                $data[$key]['uid']               = $value;
                $data[$key]['create_time']       = time();
                $data[$key]['is_type']           = 0;
                $data[$key]['is_pass']           = 1;
                $data[$key]['is_lock']           = 0;
                $data[$key]['pay_state']         = 2;
            }
            //通知申请者到微信
            Inform::sms($value,$member_miniapp_id,['title' =>'业务进展通知','type' => '身份变更','state' => '成功','content' =>'您的身份已变更为合伙人']);
        }
        if(empty($data)){
            return;
        }
        foreach ($data as $key => $value) {
            AllwinUser::editer($value['uid'],0);
        }
        return self::insertAll($data);
    }


    //创建订单
    public static function createOrder(array $param){
        $info = self::where(['member_miniapp_id' => $param['member_miniapp_id'],'id' => $param['id']])->find();
        if($info){
            self::where(['id' => $info['id']])->update(['order_no' => $param['order_no'],'price' => $param['price']]);
            return false;
        }
        $data = [
            'uid'               => $param['uid'],
            'is_pass'           => 0,
            'is_lock'           => 0,
            'is_type'           => 1,
            'order_no'          => $param['order_no'],
            'price'             => $param['price'],
            'pay_state'         => 0,
            'create_time'       => time(),
            'member_miniapp_id' => $param['member_miniapp_id']
        ];
        $rel = self::insert($data);
        return $rel ? $info : false;
    }

    public static function isLock(int $id){
        $result = self::where(['id' => $id])->field('is_lock')->find();
        $data['is_lock'] = $result['is_lock'] ? 0 : 1;
        return self::where('id',$id)->update($data);
    }

    public static function isPass(int $id,$member_miniapp_id){
        $result = self::where(['id' => $id])->field('is_pass,uid')->find();
        $data['is_pass'] = $result['is_pass'] ? 0 : 1;
        if($data['is_pass'] == 1) {
            //通知申请者到微信
            Inform::sms($result->uid,$member_miniapp_id,['title' =>'业务进展通知','type' => '合伙人申请','content' =>'您的合伙人申请已经通过审核','state' => '成功']);
        }
        return self::where('id',$id)->update($data);
    }
}